package com.chenfan.process.service.impl;

import com.alibaba.fastjson.JSONObject;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.chenfan.process.common.constants.CacheConstants;
import com.chenfan.process.entity.po.ProcessPo;
import com.chenfan.process.repository.ProcessRepository;
import com.chenfan.process.service.ProcessService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.stereotype.Service;

import java.util.List;
import java.util.Objects;
import java.util.concurrent.TimeUnit;

/**
 * @author K
 * @date 2021/3/4
 */
@Service
public class ProcessServiceImpl extends ServiceImpl<ProcessRepository, ProcessPo> implements ProcessService {

    @Autowired
    private RedisTemplate redisTemplate;

    @Override
    public List<ProcessPo> listOrCacheNodes() {
        Object o = redisTemplate.opsForValue().get(CacheConstants.NODE_CACHE);
        if (Objects.isNull(o)) {
            List<ProcessPo> list = this.list();
            redisTemplate.opsForValue().set(CacheConstants.NODE_CACHE,  JSONObject.toJSONString(list), 90, TimeUnit.MINUTES);
            return list;
        }
        return JSONObject.parseArray(o.toString(), ProcessPo.class);
    }
}
